package com.macro.mall.portal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.HcHandleConventionMapper;
import com.macro.mall.model.HcHandleConvention;
import com.macro.mall.portal.service.HcHandleConventionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * HcHandleConvention的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class HcHandleConventionServiceImpl implements HcHandleConventionService {

    private boolean isDebugEnabled;

    @Autowired
    private HcHandleConventionMapper hcHandleConventionMapper;
    // TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化

    @Override
    public List<HcHandleConvention> find(HcHandleConvention value, Integer pageNum, Integer pageSize) {
        //TODO这里可以做通过Assist做添加查询
        List<HcHandleConvention> result = null;
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("create_time desc");
        Assist assist = new Assist();
        try {
            if (!StrUtil.isBlankIfStr(value.getHandleId())) {
                assist.andEq("handle_id", value.getHandleId());
            }
            if (!StrUtil.isBlankIfStr(value.getUserName())) {
                assist.andEq("user_name", value.getUserName());
            }
            if (!StrUtil.isBlankIfStr(value.getMobile())) {
                assist.andEq("mobile", value.getMobile());
            }
            result = hcHandleConventionMapper.selectHcHandleConvention(assist);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取HcHandleConvention数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public HcHandleConvention findOne(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过HcHandleConvention的id获得HcHandleConvention对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过HcHandleConvention的id获得HcHandleConvention对象-->失败:id不能为空");
        }
        HcHandleConvention result = null;
        try {
            result = hcHandleConventionMapper.selectHcHandleConventionById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过HcHandleConvention的id获得HcHandleConvention对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(HcHandleConvention value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将HcHandleConvention中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = 0;
        try {
            result = hcHandleConventionMapper.insertNotNullHcHandleConvention(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将HcHandleConvention中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(HcHandleConvention value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过HcHandleConvention的id更新HcHandleConvention中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过HcHandleConvention的id更新HcHandleConvention中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        try {
            result = hcHandleConventionMapper.updateNotNullHcHandleConventionById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过HcHandleConvention的id更新HcHandleConvention中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过HcHandleConvention的id删除HcHandleConvention-->失败:id不能为空");
            }
            Asserts.fail("执行通过HcHandleConvention的id删除HcHandleConvention-->失败:id不能为空");
        }
        int result = 0;
        try {
            result = hcHandleConventionMapper.deleteHcHandleConventionById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过HcHandleConvention的id删除HcHandleConvention-->结果:", result);
        }
        return CommonResult.success(null);
    }


}